import { _decorator, Button, Label, size, Sprite, Tween, tween, UITransform } from 'cc';
import { Ab_BasePopUp_bA } from '../../../A-FRAME/component/ui.pop-up';
import { Ab_ClientAPI_bA } from '../../api/client.api';
import { Ab_AudioManager_bA } from '../../manager/audio-manager';
import Ab_GameDC_bA from '../../manager/data.game.center';
import Ab_GameAssets_bA from '../../manager/game.assets';
import type { Ab_Popup_bA } from '../page/popup';
const { ccclass, property } = _decorator;

@ccclass('Ab_FruitDiamond_bA')
export class Ab_FruitDiamond_bA extends Ab_BasePopUp_bA<typeof Ab_Popup_bA, number> {
	protected Ab_default_return_bA: number;

	private Ab_light_bA: UITransform;
	private Ab_lbl_diamond_bA: Label;
	private Ab_icon_diamond_bA: UITransform;

	private Ab_diamond_bA: number;

	protected Ab_initAfterOnLoad_bA(){
		this.Ab_light_bA = this.node.getChildByName('light').getComponent(UITransform);
		this.Ab_lbl_diamond_bA = this.node.getChildByName('lbl-diamond').getComponent(Label);
		this.Ab_icon_diamond_bA = this.node.getChildByName('icon-diamond').getComponent(UITransform);

		this.Ab_renderLocalUI_bA();
	}

	protected Ab__click_event__bA: { [name: string]: (this: Ab_FruitDiamond_bA, button: Button) => void; } = {
		reap(btn){
			btn.interactable = false;
			this.Ab_exit_bA(false).then(()=>btn.interactable = true);
		},
		video(btn){
			btn.interactable = false;
			this.Ab_exit_bA(true).then(()=>btn.interactable = true);
		},
	}

	public Ab_onShow_bA(diamond: number){
		this.Ab_diamond_bA = diamond;

		this.Ab_lbl_diamond_bA.string = diamond.toString();

		Ab_BasePopUp_bA.Ab_jelly_enter_bA(this.node);
		this.Ab_enter_bA();

		Ab_AudioManager_bA.Ab_playEffect_bA(Ab_AudioManager_bA.Ab_EAudio_bA.Ab_BGM_DIAMOND_bA);
	}

	private Ab_enter_bA(){
		Tween.stopAllByTarget(this.Ab_light_bA.node);

		this.Ab_light_bA.node.angle = 0;
		tween(this.Ab_light_bA.node)
		.by(1, {angle: 360})
		.repeatForever()
		.start();
	}

	private async Ab_exit_bA(video: boolean){
		let diamond = this.Ab_diamond_bA;
		if(video){
			diamond = diamond * 2;
			await Ab_ClientAPI_bA.Ab_AD_bA('incentive');
		}

		if(diamond) Ab_GameDC_bA.Ab_reapDiamond_bA(diamond, this.Ab_icon_diamond_bA.node.worldPosition);

		await Ab_BasePopUp_bA.Ab_jelly_leave_bA(this.node);
		this.Ab_close_bA(diamond);
		Tween.stopAllByTarget(this.Ab_light_bA.node);
	}

	private Ab_renderLocalUI_bA(){
		let ui_title = this.node.getChildByName('title-cong_reap').getComponent(UITransform);
		let title_size = size(
			this.node.getChildByName('background-head').getComponent(UITransform).width,
			ui_title.height
		);

		let sframe = Ab_GameAssets_bA.Ab_getLangTexture_bA(0);

		let scale = Math.min(title_size.width/sframe.originalSize.width, title_size.height/sframe.originalSize.height);

		ui_title.getComponent(Sprite).spriteFrame = sframe;

		ui_title.setContentSize(
			sframe.originalSize.width*scale,
			sframe.originalSize.height*scale
		);

		this.node.getChildByName('lbl-video').getComponent(Label).string = Ab_GameAssets_bA.Ab_getLangText_bA(15);
		this.node.getChildByName('reap').getComponent(Label).string = Ab_GameAssets_bA.Ab_getLangText_bA(16);
	}
}

